package com.kk.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.kk.crowd.entity.Role;
import com.kk.crowd.service.RoleService;
import com.kk.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


//    @PreAuthorize("hasRole('部长')")
    @RequestMapping("/getRolePage")
    @ResponseBody
    public ResultEntity getRolePage(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                                    @RequestParam(value = "pageNum",defaultValue ="1") Integer pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        // 分页查询出角色信息
        PageInfo<Role> rolePage = roleService.getRolePage(keyword, pageNum, pageSize);
        // 返回数据
        return ResultEntity.successWithData(rolePage);
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(Role role){
        roleService.save(role);
        return ResultEntity.successWithoutData();
    }


    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(Role role){
        roleService.update(role);
        return ResultEntity.successWithoutData();
    }


    @RequestMapping("/removeByRoleIdArray")
    @ResponseBody
    public ResultEntity removeByRoleIdAarry(@RequestBody List<Integer> roleIdList){
        roleService.removeRole(roleIdList);
        return ResultEntity.successWithoutData();   
    }
}
